﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.VisualTrackerManager
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using System.Linq;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem
{
  public class VisualTrackerManager
  {
    [SaveableField(0)]
    private readonly MBList<TrackedObject> _trackedObjects;

    internal static void AutoGeneratedStaticCollectObjectsVisualTrackerManager(
      object o,
      List<object> collectedObjects)
    {
      ((VisualTrackerManager) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this._trackedObjects);
    }

    internal static object AutoGeneratedGetMemberValue_trackedObjects(object o)
    {
      return (object) ((VisualTrackerManager) o)._trackedObjects;
    }

    public MBReadOnlyList<TrackedObject> TrackedObjects
    {
      get => (MBReadOnlyList<TrackedObject>) this._trackedObjects;
    }

    [CachedData]
    public int TrackedObjectsVersion { get; private set; }

    public VisualTrackerManager()
    {
      this._trackedObjects = new MBList<TrackedObject>();
      this.Initialize();
    }

    [LoadInitializationCallback]
    private void OnLoad(MetaData metaData) => this.Initialize();

    private void Initialize()
    {
      this.TrackedObjectsVersion = 0;
      this.CheckObjectValidities();
    }

    private void CheckObjectValidities()
    {
      foreach (TrackedObject trackedObject in this.TrackedObjects.ToList<TrackedObject>())
      {
        if (trackedObject.Object == null)
          this._trackedObjects.Remove(trackedObject);
      }
      ++this.TrackedObjectsVersion;
    }

    public void RegisterObject(ITrackableCampaignObject obj)
    {
      if (obj == null)
        return;
      bool flag = false;
      foreach (TrackedObject trackedObject in (List<TrackedObject>) this.TrackedObjects)
      {
        if (trackedObject.Object == obj)
        {
          flag = true;
          ++trackedObject.TrackerCount;
        }
      }
      if (flag)
        return;
      this._trackedObjects.Add(new TrackedObject((ITrackableBase) obj));
      ++this.TrackedObjectsVersion;
    }

    public bool CheckTracked(ITrackableBase obj)
    {
      foreach (TrackedObject trackedObject in (List<TrackedObject>) this.TrackedObjects)
      {
        if (trackedObject.Object == obj)
          return true;
      }
      return false;
    }

    public bool CheckTracked(BasicCharacterObject agentCharacter)
    {
      foreach (TrackedObject trackedObject in (List<TrackedObject>) this._trackedObjects)
      {
        if (trackedObject.Object.CheckTracked(agentCharacter))
          return true;
      }
      return false;
    }

    public void RemoveTrackedObject(ITrackableBase obj, bool forceRemove = false)
    {
      for (int index = this._trackedObjects.Count - 1; index >= 0; --index)
      {
        TrackedObject trackedObject = this._trackedObjects[index];
        if (trackedObject.Object == obj)
        {
          --trackedObject.TrackerCount;
          if (trackedObject.TrackerCount <= 0 | forceRemove)
          {
            this._trackedObjects.RemoveAt(index);
            ++this.TrackedObjectsVersion;
          }
        }
      }
    }

    private void ResetTracker()
    {
      this._trackedObjects.Clear();
      ++this.TrackedObjectsVersion;
    }
  }
}
